package chapterSystem.client.interfaceDrawing.activities;

import chapterSystem.client.ChapterSystemService;
import chapterSystem.client.callbacks.WriteSimpleEntityCallback;
import chapterSystem.client.entities.Pair;
import chapterSystem.client.model.entities.OrderEntity;
import chapterSystem.client.model.entitiesInt.Brother;
import chapterSystem.client.model.enums.ActivityType;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;

/**
 * User: Hasp
 * Date: 15.04.12
 * Time: 19:56
 */
public class WriteBattleOrderActivity extends Activity {
    final protected Activity callback;
    final protected Pair<String, Integer> receiver;
    final protected TextArea submitArea;

    protected WriteBattleOrderActivity(final Brother brother, final ActivityType type, Activity callback,
                                       Pair<String, Integer> receiver) {
        super(brother, type);
        this.callback = callback;
        this.receiver = receiver;

        submitArea = new TextArea();
        submitArea.setWidth("100%");
        submitArea.setHeight("80%");
    }

    protected ClickHandler createActivityClickHandler(final StackLayoutPanel workArea) {
        return new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {

                if (submitArea.getText().trim().length() > 0) {
                    ChapterSystemService.App.getInstance().createBattleOrder(
                            new OrderEntity(null, submitArea.getText(), receiver.second, brother.getId(), 0, null, "",
                                    false, brother.getName(), receiver.first), new WriteSimpleEntityCallback(workArea, callback));
                } else {
                    Window.alert("Order text must not be empty!");
                }
            }
        };
    }

    @Override
    public void drawActivity(final StackLayoutPanel workArea) {
        workArea.clear();

        final HTML header = new HTML("Write order");
        final VerticalPanel panel = new VerticalPanel();
        panel.setWidth("100%");

        final Label toField = new Label(receiver.first);

        submitArea.setHeight((workArea.getOffsetHeight() * 0.8) + "px");
        final Button submitButton = new Button("Submit");
        submitButton.addClickHandler(createActivityClickHandler(workArea));

        panel.add(toField);
        panel.add(submitArea);
        panel.add(submitButton);
        panel.setCellHorizontalAlignment(submitButton, HasAlignment.ALIGN_CENTER);

        workArea.add(panel, header, 4);
    }
}
